package com.k2data.k2app.controller;

import com.k2data.k2app.service.RedisService;
import com.k2data.k2app.util.DateFormatterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author cuilibo@k2data.com.cn
 * @Date 17-12-27 下午2:57.
 */
@Api(description = "test-redis")
@RestController
@RequestMapping("/redis-test")
public class TestWebController {
    @Autowired
    private RedisService redisService;

    @GetMapping(value = "/redis/getValue/{key}")
    public String rGetValue(@PathVariable("key") String key) {
        Object vString = redisService.getValue(key);
        System.out.println("redisService:" + vString);
        return "Redis Value:" + vString;
    }

    @ApiOperation(value = "设置key的value，value为string类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "Long", paramType = "path")
    })
     @GetMapping(value = "/set/{key}/{value}")
    public String rSetValue(@PathVariable("key") String key, @PathVariable("value") String value) {
        redisService.setValue(key, value);
        System.out.println("redisService:  SET OK");
        return "Redis Value: SET OK key:" + key + ",value:" + value;
    }

    @ApiOperation(value = "设置key的value,value是object类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "Object", paramType = "body")
    })
    @PostMapping(value = "/set/{key}",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String rSetObject(@PathVariable("key") String key, @RequestBody Object value) {
        redisService.set(key, value);
        System.out.println("redisService:  SET OK");
        return "Redis Value: SET OK key:" + key + ",value:" + value;
    }

    @ApiOperation(value = "获取key的value(Map/Object)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping(value = "/get/{key}")
    public String rGetObject(@PathVariable("key") String key) throws Exception {
        Map vString = redisService.get(key);
        System.out.println("redisService:" + vString);
        String ts = vString.get("ts").toString();
        Date parse = new SimpleDateFormat(DateFormatterUtil.timePattern).parse(ts);
        return vString.toString();
    }

    @ApiOperation(value = "模糊查询keys")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patten", value = "patten", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/keys")
    public Set<String> keys(@RequestParam(value = "patten",required = false) String patten) {
        if(patten == null) patten = "*";
        Set<String> keys = redisService.keys(patten);
        System.out.println("redisService:" + keys);
        return keys;
    }


    @ApiOperation(value = "模糊删除keys")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patten", value = "patten", required = false, dataType = "String", paramType = "query")
    })
    @DeleteMapping(value = "/keys")
    public String removekeys(@RequestParam(value = "patten",required = false) String patten) {
        if(patten == null) patten = "*";
        redisService.removeKeys(patten);
        return "true";
    }


    @ApiOperation(value = "设置key的value,value是object类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "String", paramType = "path")
    })
    @PostMapping(value = "/lpush/{key}/{value}",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String lpush(@PathVariable("key") String key, @PathVariable String value) {
        redisService.lpush(key, value);
        return "Redis Value: SET OK key:" + key + ",value:" + value;
    }

    @ApiOperation(value = "获取key的value(Map/Object)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "value", required = true, dataType = "string", paramType = "path")
    })
    @PostMapping(value = "/lremove/{key}/{value}")
    public  List<Object> zremove(@PathVariable("key") String key,@PathVariable String value) throws Exception {
        redisService.lremove(key, value);
        List<Object> lrange = redisService.lrange(key, 0, 1000);
        return lrange;
    }

    @ApiOperation(value = "获取key的value(Map/Object)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "string", paramType = "path")
    })
    @PostMapping(value = "/rlange/{key}")
    public  List<Object> rlange(@PathVariable("key") String key) throws Exception {
        List<Object> lrange = redisService.lrange(key, 0, 1000);
        return lrange;
    }

}
